/*const utils = require('utils')
Page({
    data: {
        meters: 0,
        seconds: 0,
        latitude: 0,
        longitude: 0,
        running: false,
        interval: 1000,
        markers: []
    },
    curLocation() {
        wx.getLocation({
            type: 'gcj02'
        }).then(res => {
            console.log(res)
            let {
                latitude,
                longitude
            } = res
            this.setData({
                latitude,
                longitude
            })
        })
    },
    onLoad() {
        this.curLocation()
        setInterval(this.record,this.data.interval)
    },
    test() {
        let dis = utils.getDistance(this.data.latitude, this.data.longitude, 23.3830, 113.4495)
        this.setData({
            meters: dis
        })
    },
    run() {
        this.setData({
            running: !this.data.running
        })
    },
    record() {
        if (!this.data.running) {
            return
        }
        this.setData({
            seconds: this.data.seconds + this.data.interval / 1000
        })
        wx.getLocation({
            type: 'gcj02'
        }).then(res => {
            let newMarker = {
                latitude: res.latitude,
                longitude: res.longitude,
                iconPath: 'redPoint.png',
                width: 10,
                height: 10,
            }
            let pace = 0
            let markers = this.data.markers
            if (this.data.markers.length > 0) {
                let lastmarker = this.data.markers.slice(-1)[0]
                console.log(lastmarker)
                console.log(newMarker)
                pace = utils.getDistance(lastmarker.latitude, lastmarker.longitude, newMarker.latitude, newMarker.longitude)
                if (pace > 15) {
                    markers.push(newMarker)
                }
            } else {
                markers.push(newMarker)
            }
            this.setData({
                latitude: res.latitude,
                longitude :res.longitude,
                markers,
                meters: this.data.meters + pace
            })
        })
    },
    clear(){
        this.setData({
            markers:[],
            meters:0,
            seconds:0
        })
    }
})
*/
const utils = require('utils.js')
let fmarkers = []
let timer = 0
let lemeters = 0
let lseconds =0
Page({
  data:{
    running: false,
    latitude: 0,
    longitude: 0,
    meters: 0,
    seconds: 0,
    markers: [],
    polyline: [{
      points: [],
      color: '#ff0000DD'
    }],
    interval: 5000,
    feedbackrate:500
  },
  run(){
    this.setData({
      running: !this.data.running
    })
  },
  onLoad() {
    wx.getLocation({
      type: 'gcj02',
    }).then(res => {
      console.log(res)
      this.setData({
        latitude: res.latitude,
        longitude: res.longitude
      })
    })
    setInterval(this.record,  this.data.interval)
  },
  record() {
    if (!this.data.running) {
      return
    }
    this.setData({
      seconds:this.data.seconds + this.data.interval / 1000
    })
    wx.getLocation({
      type: 'gcj02',
    }).then(res => {
      let newMarker = {
        latitude: res.latitude,
        longitude:res.longitude,
        iconPath: 'redPoint.png',
        width:10,
        height:10
      }
      let pace = 0
      let lmarkers = this.data.markers
      if (lmarkers.length > 0) {
        let lastmarker = lmarkers.slice(-1) [0]
        pace = utils.getDistance(lastmarker.latitude, lastmarker.longitude, newMarker.latitude, newMarker.longitude)
        console.log(pace)
      }
      lmarkers.push(newMarker)
      this.setData({
        latitude: res.latitude,
        longitude: res.longitude,
        markers: lmarkers,
        meters: this.data.meters + pace
      })

    })
  },
  clear() {
    this.setData({
      running: false,
      latitude: 0,
      longitude: 0,
      meters: 0,
      seconds: 0,
      markers: [],
      polyline: [{
          points: [],
          color:'#ff0000DD',
          width:2
      }],
      interval:1000,
    })
  },
  save(){
    wx.setStorage({
      data:{
       markers:this.data.markers,
      seconds:this.data.seconds,
        meters:this.data.meters
      },
      key: 'running',
    }).then(()=>{
      wx.showToast({
        title: '保存成功',
      })
    })
  },
  playback(){
    this.clear()
    wx.getStorage({
      key: 'running',
    }).then(res=>{
      fmarkers = res.data.markers
      lseconds = res.data.seconds
      lemeters = res.data.meters
    })
    timer = setInterval(this.feedback,this.data.feedbackrate)
  },
  feedback(){
    let lmarkers = this.data.markers
    let lpolyline = this.data.polyline
    if(fmarkers.length>0){
      lmarkers.push(fmarkers.shift())
      lpolyline[0].points = lmarkers
    }else{
      clearInterval(timer)
    }
    this.setData({
      markers:lmarkers,
      polyline:lpolyline,
      seconds:lseconds,
      meters:lemeters
    })
  }
})
